<template lang="pug">
.list
  .header
    .header-search
      el-input(placeholder="材料名称/发布者" prefix-icon="el-icon-search" v-model="searchString" @change="changeSearch")
      el-select(placeholder="选择材料品牌" prefix-icon="el-icon-search" clearable v-model="brandId" @change="changeBrand")
        el-option(v-for="item in brandList", :key="item.id", :label="item.name", :value="item.id")
      el-select(placeholder="选择审核状态" prefix-icon="el-icon-search" clearable v-model="status" @change="changeStatus")
        el-option(label="待审核", value="1")
        el-option(label="审核通过", value="2")
        el-option(label="审核不通过", value="3")
      el-select(placeholder="选择材料分类" prefix-icon="el-icon-search" clearable v-model="classificationId" @change="changeCategory")
        el-option(v-for="item in cateList", :label="item.title", :value="item.id", :key="item.id")
      el-select(placeholder="选择上下架情况" prefix-icon="el-icon-search" clearable v-model="isOnSales" @change="changeSales")
        el-option(label="已上架", :value="true")
        el-option(label="已下架", :value="false")
    .header-btn
      el-button(type="primary" size="small" @click="goAuditList")
        span 审核列表
        .el-icon-arrow-right.el-icon--right
      el-button(icon="el-icon-setting" size="small" @click="settingHeader")
  .list-table
    el-table(
      height="100%"
      :data="list"
      :header-cell-style="{ fontSize: '14px', color: '#64698F', background: '#F8F7FC' }"
      border
      style="width: 100%"
      v-loading="loading"
      empty-text="未找到符合条件的内容"
    )
      el-table-column(v-if="currentList.length" type="index" width="55" align="center" label="序号")
      el-table-column(
        v-for="item, index in currentList"
        :key="index"
        :label="item.name"
        :min-width="item.minWidth"
        :sortable="item.sortable"
        :align="item.align"
        :prop="item.key"
      )
        template(slot-scope="scope")
          .table-item-bd.flex.click(v-if="item.key === 'materialName'" @click="seeDetail(scope.row)")
            .table-item-name {{scope.row[item.key]}}
          //- .table-item-bd(v-else-if="item.key === 'companyType'") {{scope.row[item.key] | filterSupplier}}
          .table-item-bd(v-else-if="item.key === 'status'")
            .table-item-status(v-if="scope.row[item.key] == 1")
              label.status-point.normal
              span 待审核
            .table-item-status(v-if="scope.row[item.key] == 2")
              label.status-point.success
              span 通过
            .table-item-status(v-if="scope.row[item.key] == 3")
              label.status-point.warning
              span 不通过
            .table-item-status(v-if="scope.row[item.key] == 4")
              label.status-point.warning
              span 信息不全
          .table-item-bd(v-else-if="item.key === 'isOnSales'")
            .table-item-sales
              .item-sales.success(v-if="scope.row[item.key] && scope.row.status == 2 && $permission('AB-04001-002')" @click="changeSalesStatus(scope.row)")
                span 已上架
                .iconfont.el-icon-arrow-down
              .item-sales.warning(v-if="!scope.row[item.key] && scope.row.status == 2 && $permission('AB-04001-002')" @click="changeSalesStatus(scope.row)")
                span 已下架
                .iconfont.el-icon-arrow-down
              .item-sales.info(v-if="!scope.row[item.key] && scope.row.status != 2 && $permission('AB-04001-002')" @click="changeSalesStatus(scope.row)")
                span 已下架
                .iconfont.el-icon-arrow-down
          .table-item-bd(v-else-if="item.key === 'isHot'")
            el-switch(v-model="scope.row[item.key]" disabled active-color="#8676FF" inactive-color="#B2B4C7")
          .table-item-bd(v-else-if="item.key === 'price'") ￥{{scope.row.lowPrice === scope.row.highPrice ? `${scope.row.lowPrice}` : `${scope.row.lowPrice}-${scope.row.highPrice}`}}
          .table-item-bd(v-else-if="item.key === 'finalPrice'") ￥{{scope.row.minFinalPrice === scope.row.maxFinalPrice ? `${scope.row.minFinalPrice}` : `${scope.row.minFinalPrice}-${scope.row.maxFinalPrice}`}}
          .table-item-bd(v-else-if="item.key === 'createTime'") {{new Date(scope.row[item.key]).format('yyyy-MM-dd')}}
          .table-item-bd(v-else-if="item.key === 'handle'")
            .table-item-handle
              .table-item-btn.info(@click="delItem(scope.row)" v-if="$permission('AB-04001-003')") 删除
          .table-item-text(v-else) {{scope.row[item.key]}}
  .table-pagination
    el-pagination(
      background
      layout="prev, pager, next, jumper"
      :total="total"
      :current-page="page"
      :page-size="pageSize"
      @current-change="currentChange"
    )
  set-table-header(
    :show="showDialog"
    :allHeaderList="allHeaderList"
    :defaultList="defaultList"
    :currentList="currentList"
    :filterHeaders="filterHeaders"
    type="5"
    @close="closeDialog"
    @confirm="confirmDialog"
  )
</template>

<script>
import { getMaterialList, getTableMenu, getCategoryList, getBrandList, putOnOrOff, deleteMaterial } from 'api'
import setTableHeader from '@/components/modal/setTableHeader'
export default {
  name: 'materialList',
  data () {
    return {
      page: 1,
      pageSize: 20,
      total: 0,
      searchString: '',
      brandId: '',
      status: '',
      classificationId: '',
      isOnSales: '',
      list: [],
      loading: false,
      showDialog: false,
      allHeaderList: [
        { name: "分类名称", key: 'classificationName' },
        { name: "发布者", key: 'companyName' },
        { name: "品牌", key: 'brandName' },
        { name: "审核状态", key: 'status' },
        { name: "上架状态", key: 'isOnSales' },
        { name: "是否热门", key: 'isHot' },
        { name: "提交时间", key: 'createTime', sortable: true },
        { name: "市场价", key: 'price' },
        { name: "成交价", key: 'finalPrice' },
        { name: "库存量", key: 'stock' },
        { name: "浏览量", key: 'viewAmount' }
      ],
      filterHeaders: ['materialName', 'handle'],
      defaultList: [
        { name: "材料名称", key: 'materialName', isDisable: true, minWidth: 100 },
        { name: "分类名称", key: 'classificationName' },
        { name: "发布者", key: 'companyName' },
        { name: "品牌", key: 'brandName' },
        { name: "审核状态", key: 'status' },
        { name: "上架状态", key: 'isOnSales' },
        { name: "是否热门", key: 'isHot' },
        { name: "提交时间", key: 'createTime', sortable: true },
        { name: "操作", key: 'handle', isDisable: true }
      ],
      currentList: [],
      cateList: [],
      brandList: []
    }
  },
  components: {
    setTableHeader
  },
  methods: {
    changeSearch () {
      this.page = 1
      this.getList()
    },
    changeBrand () {
      this.page = 1
      this.getList()
    },
    changeStatus () {
      this.page = 1
      this.getList()
    },
    changeCategory () {
      this.page = 1
      this.getList()
    },
    changeSales () {
      this.page = 1
      this.getList()
    },
    seeDetail (row) {
      this.$router.push({ name: 'materialDetail', params: { id: row.materialId } })
    },
    async changeSalesStatus (row) {
      try {
        if (!row.isOnSales && row.status != 2) return
        else {
          await this.$axios.post(putOnOrOff, { materialId: row.materialId, isOnSales: !row.isOnSales })
          if (row.isOnSales) this.$message.success('下架成功！')
          else  this.$message.success('上架成功！')
          this.getList()
        }
      } catch (err) {
        console.log(err)
        this.$message.error(err.message || err.ResultSet.errors)
      }
    },
    delItem (row) {
      this.$confirm('是否确定删除该材料？删除后该材料将下架', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(async () => {
        try {
          await this.$axios.post(deleteMaterial, { materialId: row.materialId })
          this.$message.success('删除成功！')
          this.getList()
        } catch (err) {
          console.log(err)
          this.$message.error(err.message || err.ResultSet.errors)
        }
      })
    },
    goAuditList () {
      this.$router.push({ name: 'materialAuditList' })
    },
    currentChange (page) {
      this.page = page
      this.getList()
      // this.$router.replace({ name: this.$route.name, query: { ...this.$route.query, page: this.page } })
    },
    settingHeader () {
      this.showDialog = true
    },
    closeDialog () {
      this.showDialog = false
    },
    confirmDialog () {
      this.showDialog = false
      this.getTableMenu()
    },
    async getList () {
      try {
        this.loading = true
        let query = { ...this.$route.query }
        query.currentPage = this.page
        query.pageLines = this.pageSize
        if (this.searchString) query.searchString = this.searchString
        if (this.brandId) query.brandId = this.brandId
        if (this.status) query.status = this.status
        if (this.classificationId) query.classificationId = this.classificationId
        if (this.isOnSales !== '') query.isOnSales = this.isOnSales
        const { ResultSet } = await this.$axios.post(getMaterialList, { ...query })
        // this.page = this.$route.query.page || 1
        this.total = ResultSet.recordNumber
        this.list = ResultSet.userData.result
        this.loading = false
      } catch (err) {
        this.loading = false
        console.log(err)
        this.$message.error(err.message || err.ResultSet.errors)
      }
    },
    async getTableMenu () {
      try {
        this.loading = true
        const { ResultSet } = await this.$axios.post(getTableMenu, { type: 5 })
        this.currentList = ResultSet.userData.data ? JSON.parse(ResultSet.userData.data) : this.defaultList
        await this.getList()
      } catch (err) {
        this.loading = false
        console.log(err)
        this.$message.error(err.message || err.ResultSet.errors)
      }
    },
    async getCategoryList () {
      try {
        const { ResultSet } = await this.$axios.post(getCategoryList)
        this.cateList = ResultSet.userData.result
      } catch (err) {
        console.log(err)
        this.$message.error(err.message || err.ResultSet.errors)
      }
    },
    async getBrandList () {
      try {
        const { ResultSet } = await this.$axios.post(getBrandList)
        this.brandList = ResultSet.userData.loadBrandList
      } catch (err) {
        console.log(err)
        this.$message.error(err.message || err.ResultSet.errors)
      }
    },
    async init () {
      await this.getTableMenu()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.getCategoryList()
      vm.getBrandList()
      vm.init()
    })
  },
  beforeRouteUpdate (to, from, next) {
    next()
    this.init()
  }
} 
</script>

<style lang="scss" scoped>
.list {
  width: 100%;
  height: 100%;
  background: #FFFFFF;
  border: 1px solid #F3F3F4;
  box-sizing: border-box;
  box-shadow: 0px 4px 0px #F3F3F4;
  border-radius: 10px;
  padding: 0 40px;
}
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 0;
  .el-input {
    width: 202px;
    margin-right: 16px;
  }
  .el-select {
    margin-right: 16px;
  }
  .header-search {
    display: flex;
    align-items: center;
  }
  .header-info {
    font-size: 14px;
    line-height: 22px;
    color: #8B8EAB;
  }
}
.list-table {
  height: calc(100% - 150px);
}
.table-pagination {
  padding: 24px 0;
  text-align: right;
}
.table-item-bd {
  &.flex {
    display: flex;
    align-items: center;
  }
  &.click {
    cursor: pointer;
  }
  .table-item-head {
    width: 24px;
    height: 24px;
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
    border-radius: 4px;
    margin-right: 8px;
  }
  .table-item-name {
    font-size: 14px;
    line-height: 22px;
    color: #FF2E42;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .table-item-sales {
    display: flex;
    align-items: center;
  }
  .item-sales {
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 5px 8px;
    border: 1px solid #F7DCB7;
    box-sizing: border-box;
    border-radius: 2px;
    &.success {
      border-color: #C0E5D1;
      background: #F2FAF6;
      color: #32C37D;
    }
    &.warning {
      border-color: #F7DCB7;
      background: #FFF7EC;
      color: #FC9700;
    }
    &.info {
      border-color: #B2B4C7;
      background: #F6F6F9;
      color: #8B8EAB;
    }
    span {
      display: inline-block;
      font-size: 14px;
      line-height: 22px;
    }
    .iconfont {
      margin-left: 6px;
      font-size: 16px;
      color: #CCD2E3;
    }
  }
}
.table-item-status {
  display: flex;
  align-items: center;
  .status-point {
    position: relative;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    margin-right: 4px;
    &:before {
      content: '';
      position: absolute;
      top: 50%;
      left: 50%;
      width: 4px;
      height: 4px;
      transform: translate(-50%, -50%);
    }
    &.success {
      background: rgba(48, 217, 135, .5);
      &:before {
        background: #30D987;
      }
    }
    &.warning {
      background: rgba(251, 177, 66, .5);
      &:before {
        background: #FBB142;
      }
    }
    &.normal {
      background: rgba(134, 118, 255, .5);
      &:before {
        background: #8676FF;
      }
    }
  }
  .iconfont {
    font-size: 16px;
    margin-right: 8px;
  }
  .icon-normal {
    color: #32C37D;
  }
  .icon-block-up {
    color: #FBB142;
  }
}
.table-item-handle {
  display: flex;
  .table-item-btn {
    margin-right: 8px;
    padding: 5px 8px;
    font-size: 12px;
    line-height: 20px;
    border-radius: 6px;
    cursor: pointer;
    &.warning {
      background: #FDEFF1;
      color: #FF2E42;
    }
    &.normal {
      background: #F3F0FF;
      color: #7362F0;
    }
    &.info {
      background: #F6F6F9;
      color: #3E4373;
    }
  }
}
</style>
